package com.athuiju.hr.service.impl;

import com.athuiju.hr.entity.Performance;
import com.athuiju.hr.entity.vo.PerBilityVo;
import com.athuiju.hr.entity.vo.PerIdeaVo;
import com.athuiju.hr.entity.vo.PerformanceVo;
import com.athuiju.hr.entity.vo.PerformanceVoQuery;
import com.athuiju.hr.mapper.PerformanceMapper;
import com.athuiju.hr.service.PerformanceService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.DecimalFormat;
import java.text.MessageFormat;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wzh
 * @since 2022-01-19
 */
@Service
public class PerformanceServiceImpl extends ServiceImpl<PerformanceMapper, Performance> implements PerformanceService {

    @Autowired
    PerformanceMapper performanceMapper;

    //    根据员工id查询员工工作能力绩效
    @Override
    public List<PerBilityVo> getPerBilityById(String id) {
        return performanceMapper.getPerBilityById(id);
    }

    //    根据员工id查询员工工作理念绩效
    @Override
    public List<PerIdeaVo> getPerIdeaById(String id) {
        return performanceMapper.getPerIdeaById(id);
    }



    /*
     * 根据员工id查询员工绩效
     * */
    @Override
    public List<PerformanceVo> getByProId(String id) {
        return performanceMapper.getByProId(id);
    }

//    查询相同绩效工作理念信息
    @Override
    public List<Performance> getPerformance() {
        return performanceMapper.getPerformance();
    }


    /*
     * 绩效总览分页
     * */
    @Override
    public List<PerformanceVo> pageQuery(Page<PerformanceVo> pageSture, PerformanceVoQuery performanceVoQuery) {
        QueryWrapper<PerformanceVo>queryWrapper=new QueryWrapper<>();
        //当模糊查询条件为null时，就显示pageSture里面的所有数据
        if (performanceVoQuery==null){
          List<PerformanceVo> list= baseMapper.selectMyPage(pageSture, null);
            return list;
        }
        String yearIdea = performanceVoQuery.getYearIdea();
        Integer quarter = performanceVoQuery.getQuarter();
        String sector = performanceVoQuery.getSector();


        //年份筛选
        if (!StringUtils.isEmpty(yearIdea)){
            queryWrapper.like("performance.year_idea",yearIdea);
        }
        //季度筛选
        if (!StringUtils.isEmpty(quarter)){
            queryWrapper.like("performance.quarter",quarter);
        }
        //部门筛选
        if (!StringUtils.isEmpty(sector)){
            queryWrapper.like("`profiles`.sectorid",sector);
        }
        //假删除筛选条件
        queryWrapper.eq("`profiles`.is_deleted",0);
        //在职人员筛选条件
        queryWrapper.isNull("`profiles`.gmt_departure");
        //日期倒序排序
        queryWrapper.orderByDesc("performance.gmt_modified");
        List<PerformanceVo> list=  baseMapper.selectMyPage(pageSture,queryWrapper);

        for (int i=0;i<list.size();i++){
            if (list.get(i).getTeamwork()!=null||list.get(i).getDedicated()!=null||list.get(i).getServe()!=null||list.get(i).getHug()!=null) {
                list.get(i).setGpaIdea((((list.get(i).getTeamwork() + list.get(i).getDedicated() + list.get(i).getServe() + list.get(i).getHug())/4) / 10 - 5) );//工作理念绩效
                list.get(i).setAvgIdea((list.get(i).getTeamwork() + list.get(i).getDedicated() + list.get(i).getServe() + list.get(i).getHug())/4 );//工作理念平均分
            }
            if(list.get(i).getCode()!=null ||list.get(i).getEgo()!=null||list.get(i).getTask()!=null||list.get(i).getQuality()!=null||list.get(i).getSolve()!=null||list.get(i).getReport()!=null){
                list.get(i).setGpaBility((((list.get(i).getCode() + list.get(i).getEgo() +  list.get(i).getTask()+list.get(i).getQuality()+list.get(i).getSolve()+list.get(i).getReport())/6) / 10 - 5));//工作能力绩效
                list.get(i).setAvgBility((list.get(i).getCode() + list.get(i).getEgo()  + list.get(i).getTask()+list.get(i).getQuality()+list.get(i).getSolve()+list.get(i).getReport())/6);//工作能力绩效平均分

            }
            if(list.get(i).getGpaBility()!=null&&list.get(i).getGpaIdea()!=null&&list.get(i).getTeamwork()!=null&&list.get(i).getDedicated()!=null&&list.get(i).getServe()!=null&&list.get(i).getHug()!=null&&list.get(i).getCode()!=null &&list.get(i).getEgo()!=null&&list.get(i).getTask()!=null&&list.get(i).getQuality()!=null&&list.get(i).getSolve()!=null&&list.get(i).getReport()!=null){
                    list.get(i).setSumGpa((list.get(i).getGpaIdea() + list.get(i).getGpaBility())/2);//总绩效
            }
        }
        return list;
    }


    /*
     * 删除工作能力绩效
     * */
    @Override
    public boolean updatePerById(String id) {
        return performanceMapper.updatePerById(id);
    }


    /*
     * 删除工作理念绩效
     * */
    @Override
    public boolean updatePerIdeaById(String id) {
        return performanceMapper.updatePerIdeaById(id);
    }




}
